﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LY.Interface;
using LY.Entity;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
using LY.Entity.Jsons;

namespace QJ.CMS.Areas.Admin.Controllers
{
    public class ArticleController : Controller
    {
        //
        // GET: /Article/
        private static IArticleService _aservice;
        public ArticleController(IArticleService aService)
        {
            _aservice = aService;
        }

        public ActionResult Index()
        {
            return View();
        }

        #region 文章
        /// <summary>
        /// 文章列表页面
        /// </summary>
        /// <returns></returns>
        public ActionResult ArticleList()
        {
            return View();
        }

        /// <summary>
        /// 文章列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ArticleList(ArticleSearch search, FormCollection form)
        {
            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];
            PagedList<C_Article> waList = _aservice.QueryArticleList(search, view);
            JsonQTable at = JsonQTable.ConvertFromPagedList<C_Article>(waList, colkey, colsinfo.Split(','));
            return Json(at);
        }

        /// <summary>
        /// 文章新增或编辑操作，返回不同状态界面
        /// </summary>
        /// <param name="id"></param>
        /// <param name="menuId"></param>
        /// <returns></returns>
        [ValidateInput(false)]
        public ActionResult ArticleEdit(int? id, string  menuId=null)
        {
            C_Article article = null;
          
            if (id.HasValue)
            {
                article = _aservice.GetArticle(id.Value);
                if (article.Sequence == null)
                {
                    article.Sequence = 0;
                }
                if (string.IsNullOrEmpty(article.PicturesPath))
                {
                    article.PicturesPath = @"~/Content/assets/img/noimg.jpg";
                }
                if (article.PublishTime == null)
                {
                    article.PublishTime = DateTime.Now;
                }
            }
            else
            {
                article = new C_Article();
                article.Sequence = 0;
                article.PicturesPath = @"~/Content/assets/img/noimg.jpg";
                article.PublishTime = DateTime.Now; 
                var loginUserInfo = System.Web.HttpContext.Current.Session["LoginUseInfo"];
                article.Author = ((LY.Entity.S_User)loginUserInfo).UserName;
                if (!string.IsNullOrEmpty(menuId))
                {
                    article.MenuId = Convert.ToInt32(menuId);
                }
             
            }
            return View(article);
        }

        /// <summary>
        /// 新增或编辑文章信息保存操作
        /// </summary>
        /// <param name="id"></param>
        /// <param name="Menu"></param>
        /// <param name="UpperID"></param>
        /// <param name="TitlePic"></param>
        /// <param name="Pictures"></param>
        /// <param name="PicturesPath"></param>
        /// <param name="article"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public JsonResult ArticleSave(int? id, string Menu, int? MenuId, string TitlePic, string Pictures, string PicturesPath, C_Article article, FormCollection collection)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                string errmsg = "";
                if (!Validate(id, Menu, MenuId, TitlePic, Pictures, PicturesPath, article, out errmsg))
                {
                    rMsg.Status = -1;
                    rMsg.Message = errmsg;
                }
                else
                {
                    int ret = _aservice.SaveArticle(article);
                    if (ret > 0)
                    {
                        rMsg.Status = 1;
                        rMsg.Message = "操作成功！";
                    }
                    else
                    {
                        rMsg.Status = -1;
                        rMsg.Message = "意外错误，请稍后重试！";
                    }
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            rMsg.Data = Url.Action("Index", new { menu = MenuId });
            return Json(rMsg);
        }

        /// <summary>
        /// 输入文章信息验证
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="errmsg"></param>
        /// <returns></returns>
        private static bool Validate(int? id, string Menu, int? UpperID, string TitlePic, string Pictures, string PicturesPath, C_Article article, out string errmsg)
        {
            errmsg = "";
            if (article == null)
            {
                errmsg = "参数错误！";
                return false;
            }
            //if (Menu != "" && UpperID != null && UpperID != 0)
            //{
            //    article.Menu = Menu;
            //    article.MenuId = UpperID;
            //}
            //else
            //{
            //    errmsg = "请选择栏目！";
            //    return false;
            //}
            if (article.Sequence == null)
            {
                article.Sequence = 0;
            }
            if (!string.IsNullOrEmpty(TitlePic) && !string.IsNullOrEmpty(Pictures) && !string.IsNullOrEmpty(PicturesPath))
            {
                article.TitlePic = TitlePic;
                article.Pictures = Pictures;
                article.PicturesPath = PicturesPath;
            }
            if (id == 0)
            {
                var loginUserInfo = System.Web.HttpContext.Current.Session["LoginUseInfo"];
                article.CreateUser = ((LY.Entity.S_User)loginUserInfo).UserName;
                article.CreateUserID = ((LY.Entity.S_User)loginUserInfo).ID;
                article.CreateTime = DateTime.Now;
            }
            else
            {
                var loginUserInfo = System.Web.HttpContext.Current.Session["LoginUseInfo"];
                article.PublishUser = ((LY.Entity.S_User)loginUserInfo).UserName;
                article.PublishUserID = ((LY.Entity.S_User)loginUserInfo).ID;
                article.PublishTime = DateTime.Now;
            }
            if (article.Name == "" || article.Name == null)
            {
                errmsg = "请输入文章标题！";
                return false;
            }
            if (article.Author == "" || article.Author == null)
            {
                errmsg = "请输入文章作者！";
                return false;
            }
            if (article.Synopsis == "" || article.Synopsis == null)
            {
                errmsg = "请输入内容摘要！";
                return false;
            }
            if (article.Content == "" || article.Content == null)
            {
                errmsg = "请输入文章内容！";
                return false;
            }
            return true;
        }

        /// <summary>
        /// 文章删除操作
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ArticleDelete(int id, FormCollection collection)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                if (id <= 0)
                {
                    rMsg.Status = -1;
                    rMsg.Message = "参数错误！";
                }
                int ret = _aservice.DeleteArticle(id);
                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Message = "操作成功！";
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {

                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }
        #endregion

    }
}
